/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.teste;

import com.teste.Empregado;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Tshuba
 */
public class LoginServlet extends HttpServlet {
/**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //processRequest(request, response);
        response.setContentType("text/html");
        //PrintWriter out = response.getWriter();
       // response.setHeader("Refresh", "5; URL=/dados");
       /* String title = "Login";
        out.println("<HTML><BODY>\n"
                + "<H1 ALIGN=CENTER>" + title + "</H1>\n"
                + ""
                + "</BODY></HTML>");*/
                String email = request.getParameter("Email");
                String passw = request.getParameter("Password");

                PersistenceManager pm = PMF.get().getPersistenceManager();
                //WHERE composer = 'Lennon, John'"
		String query = "select from " + Empregado.class.getName() + " where email == '" + email+"'";

                List<Empregado> e = (List<Empregado>) pm.newQuery(query).execute();

                if( e.get(0).getPassword().equals(passw)){
                    response.sendRedirect("/dados");
                }else{
                    response.sendRedirect("login.html");
                }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
